package com.viser.partyinrs;

import java.util.ArrayList;

import com.viser.partyinrs.adapters.MestaAdapter;
import com.viser.partyinrs.entities.Mesto;
import com.viser.partyinrs.listadapters.MestaListAdapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class MestaActivity extends Activity {
	
	Button btnPretraga;
	MestaAdapter adapter;
	MestaListAdapter listAdapter;
	ArrayList<Mesto> mesta=new ArrayList<Mesto>();
	EditText txtKljucnaRec;
	ListView lvMesta;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.mesta);
        btnPretraga=(Button)findViewById(R.id.btnPretraga);
        txtKljucnaRec = (EditText)findViewById(R.id.txtPretraga);
        adapter=new MestaAdapter();
        //listAdapter=new MestaListAdapter(mesta, this);
        lvMesta=(ListView)findViewById(R.id.lvMesta);
        lvMesta.setAdapter(listAdapter);
        //listAdapter = new MestaListAdapter(mesta, this);
        btnPretraga.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mesta=adapter.getMestoEntities(txtKljucnaRec.getText().toString());
				listAdapter=new MestaListAdapter(mesta, MestaActivity.this);
				lvMesta.setAdapter(listAdapter);
				listAdapter.notifyDataSetChanged();
			}
		});
        
        lvMesta.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				Intent i = new Intent(MestaActivity.this, MestoDetaljiActivity.class);
				Mesto m = (Mesto)lvMesta.getItemAtPosition(pos);
				i.putExtra("mesto", m);
				startActivity(i);
			}
		});
        
	}
}
